package com.zhj.rabbitmq.demo4;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: rabbimmq-demo
 * @description: topic 类型交换机消息发送
 * @author: zhengjibin
 * @create: 2020-11-17 15:15
 * @Version 1.0
 **/
public class ExchangeTopicSendMessage {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        //配置rabbitmq的链接信息
        factory.setHost("122.112.198.94");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = null;// 定义rabbitmq的网络连接 ，一个连接
        Channel channel = null;//定义一个链接中的通道
        connection = factory.newConnection();//通过工厂获得连接
        channel = connection.createChannel();//通过连接获得通道
        String exchangeName = "topic_exchange";
        channel.exchangeDeclare(exchangeName,"topic",true);
        String routingkey = "aa.bb.22";

        String message = "交换机消息444";
        channel.basicPublish(exchangeName,routingkey,null,message.getBytes("UTF-8"));

        System.out.println("消息发送成功");

        channel.close();
        connection.close();
    }
}
